package com.apex.restful.service.impl;

import com.apex.common.entity.JsonArray;
import com.apex.common.entity.JsonEntity;
import com.apex.common.util.JsonUtils;
import com.apex.restful.domain.MessageApproveDomain;
import com.apex.restful.entity.MessageApprove;
import com.apex.restful.entity.RelationshipUsers;
import com.apex.restful.service.MessageApproveService;
import com.edu.orm.annotation.TargetDataSource;
import com.edu.orm.datasource.DBUtil;
import com.edu.orm.entity.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-4-7
 */
@Service
public class MessageApproveServiceImpl implements MessageApproveService {

    @Autowired
    private DBUtil dbUtil;

    @TargetDataSource("ds2")
    @Override
    public String getEntity(Integer messageId) {
        MessageApprove messageApprove=null;
        List<MessageApprove> list=dbUtil.getAllBeanList("select * from MessageApprove where messageId = ? ",MessageApprove.class,messageId);
        if(null!=list&&list.size()>0){
            messageApprove=list.get(0);
        }
        if(null!=messageApprove){
            JsonEntity<MessageApprove> jsonEntity=new JsonEntity<>();
            jsonEntity.setData(messageApprove);
            return JsonUtils.toJson(jsonEntity);
        }
        JsonEntity<MessageApprove> jsonEntity=new JsonEntity<>("error");
        return JsonUtils.toJson(jsonEntity);
    }

    @TargetDataSource("ds2")
    @Override
    public String getByUserId(String userId, Integer current, Integer rows,String title) {
        List<RelationshipUsers> list=dbUtil.getAllBeanList("" +
                "select deptid from  RelationshipUsers where UserID= ?",RelationshipUsers.class,userId);
        StringBuffer hql=new StringBuffer(" " +
                "from MessageApprove where moduletypeid=1004 and delflag=0 and ApproveFLag=1 and (seldept='' ");
        List<Object> params=new ArrayList<>();
        if(null!=list&&list.size()>0){
            for (RelationshipUsers relationshipUsers:list){
                if(!StringUtils.isEmpty(relationshipUsers.getDeptId())){
                    hql.append(" or seldept like ? ");
                    params.add("%"+relationshipUsers.getDeptId()+"%");
                }
            }
        }
        hql.append(") ");
        if(!StringUtils.isEmpty(title)&&!"null".equals(title)){
            hql.append(" and messagetitle like ? ");
            params.add("%"+title+"%");
        }
        int count=dbUtil.count("select count(*) as c  "+hql.toString()+" ",params.toArray());
        hql.append(" order by sendtime desc");
        Pager pager=new Pager();
        pager.setTotalPages(count);
        if(null==current){
            pager.setCurrent(1);
        }else {
            pager.setCurrent(current);
        }
        if(null==rows){
            pager.setRows(5);
        }else {
            pager.setRows(rows);
        }
        String sql="select messageid,messagetitle,sendtime "+hql.toString();
        List<MessageApprove> lists=dbUtil.getAllSqlServerPagerBeanList(pager,sql,MessageApprove.class,params.toArray());
        JsonArray<MessageApprove> jsonArray=new JsonArray<>();
        jsonArray.setDataList(lists);
        return JsonUtils.toJson(jsonArray);
    }
}
